package com.hyts.sdkhttp;

import com.hyts.sdkhttp.base.SdkClient;
import com.hyts.sdkhttp.base.SdkExecutor;
import com.hyts.sdkhttp.constant.CharacterEncoding;
import com.hyts.sdkhttp.constant.ContentType;
import com.hyts.sdkhttp.constant.RequestMethod;
import com.hyts.sdkhttp.domain.*;
import com.hyts.sdkhttp.error.SdkConnectionError;
import com.hyts.sdkhttp.handler.SdkRequestHandler;
import com.hyts.sdkhttp.handler.SdkResponseHandler;
import lombok.Builder;

import java.io.IOException;

/**
 * project-name:iscloud
 * package-name:com.ultrapower.zq
 * author:Libo/Alex
 * create-date:2018-03-12 9:49
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: 主要用于sdk服务调用信息,
 *             默认的操作客户端服务信息调用总体入口客户端
 */
@Builder
public final class DefaultSdk implements SdkClient, SdkExecutor {

    /* (非 Javadoc)  
     * <p>Title: post</p>  
     * <p>Description: post请求操作</p>  
     * @param request
     * @return  
     * @see com.hyts.http.base.SdkClient#post(com.hyts.http.domain.SdkRequest)  
     */  
    @Override
    public SdkResponse post(SdkRequest request) {
		try {
			SdkURLConnection connection = new SdkURLConnection();
    		request.setRequestMethod(RequestMethod.POST);
    		request.setContentType(request.getContentType() == null?ContentType.JSON_DATA:request.getContentType());
    		request.setCharset(CharacterEncoding.UTF_8);
			return SdkResponseHandler.getResultStr
					(SdkRequestHandler.open(connection.build(request)));
		} catch (SdkConnectionError | IOException e) {
			e.printStackTrace();
		} 
        return null;
    }

    /* (非 Javadoc)  
     * <p>Title: get</p>  
     * <p>Description: get请求操作</p>  
     * @param request
     * @return  
     * @see com.hyts.http.base.SdkClient#get(com.hyts.http.domain.SdkRequest)  
     */  
    @Override
    public SdkResponse get(SdkRequest request) {
    	try {
			SdkURLConnection connection = new SdkURLConnection();
    		request.setRequestMethod(RequestMethod.GET);
			request.setCharset(CharacterEncoding.UTF_8);
			request.setContentType(request.getContentType() == null?ContentType.JSON_DATA:request.getContentType());
			connection = SdkRequestHandler.open(connection.build(request));
    		/*if(request.getContentType() == ContentType.JSON_DATA) {
    		   return  
    		}*/
    		//SdkResponseHandler.getResultJson(connection);
			return SdkResponseHandler.getResultStr(connection);
		} catch (SdkConnectionError | IOException e) {
			e.printStackTrace();
		} 
        return null;
    }

    @Override
    public SdkResponse put(SdkRequest request) {
        return null;
    }

    @Override
    public SdkResponse delete(SdkRequest request) {
        return null;
    }

    @Override
    public SdkResponse head(SdkRequest request) {
        return null;
    }

    @Override
    public SdkResponse download(SdkDownloadRequest request) {
        return null;
    }

    /* (非 Javadoc)  
     * <p>Title: upload</p>  
     * <p>Description: 上传文件操作</p>  
     * @param request
     * @return  
     * @see com.hyts.http.base.SdkExecutor#upload(com.hyts.http.domain.SdkRequest)  
     */  
    @Override
    public SdkResponse upload(SdkUploadRequest request) {
		try {
			SdkURLConnection connection = new SdkURLConnection();
			request.setRequestMethod(RequestMethod.POST);
			request.setContentType(ContentType.MULTIPART_DATA);
			request.setCharset(CharacterEncoding.UTF_8);
			return SdkResponseHandler.getResultStr
					(SdkRequestHandler.open(connection.build(request)));
		} catch (SdkConnectionError | IOException e) {
			e.printStackTrace();
		}
        return null;
    }

	/* (非 Javadoc)  
	 * <p>Title: get</p>  
	 * <p>Description: </p>  
	 * @param url
	 * @return  
	 * @see com.hyts.http.base.SdkClient#get(java.lang.String)  
	 */  
	@Override
	public SdkResponse get(String url) {
		try {
			SdkURLConnection connection = new SdkURLConnection();
			SdkRequest request = new SdkRequest();
			request.setUrl(url);
    		request.setRequestMethod(RequestMethod.GET);
			request.setContentType(request.getContentType() == null?ContentType.JSON_DATA:request.getContentType());
			request.setCharset(CharacterEncoding.UTF_8);
			return SdkResponseHandler.getResultStr
					(SdkRequestHandler.open(connection.build(request)));
		} catch (SdkConnectionError | IOException e) {
			e.printStackTrace();
		} 
        return null;
	}
}
